﻿using UnityEngine;
using System.Collections.Generic;

public class CommodityConfig : MonoBehaviour {
	public List<CommodityTemplet> commodities;

	private static CommodityConfig g_instance;

	public static CommodityConfig Instance {
		get {
			return g_instance;
		}
	}

	void Awake()
	{
		g_instance = this;
	}

	void OnDestroy()
	{
		g_instance = null;
	}

	public List<Commodity> generate(int num = 5)
	{
		if (num < 1 || num > commodities.Count){
			Debug.LogError("Error num:" + num);
			return null;
		}

		List<CommodityTemplet> commodityTempletList = new List<CommodityTemplet>(commodities.ToArray());
		int removeCount = commodities.Count - num;
		for (int i=0; i<removeCount; i++){
			int removeIndex = Random.Range(0, commodityTempletList.Count);
			commodityTempletList.RemoveAt(removeIndex);
		}

		return instantiate(commodityTempletList);
	}

	public List<Commodity> instantiate(List<CommodityTemplet> templetList)
	{
		List<Commodity> commodityList = new List<Commodity>(templetList.Count);

		for (int i=0; i<templetList.Count; i++){
			commodityList.Add(templetList[i].instantiate());
		}

		return commodityList;
	}
}
